package mediadisplay.ui.colors 
{
	import flash.display.Bitmap;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.ui.Mouse;
	/**
	 * ...
	 * @author Anders Figenschow selvfølgelig
	 */
	public class ColorMap extends Sprite
	{
		[Embed(source='../../../../bin/colormap.jpg')]
		public static var ColormapImage:Class;
		private var gfx:Bitmap;
		private var colorpicker:ColorPicker;
		public var chosenColor:uint = 0;
		public static const COLOR_CHOSEN:String = "colorChosen";
		private var shortcuts:Array;
		private var mouseOverShortcut:Boolean = false;
		private var shortcutOver:int = -1;
		
		public function ColorMap() 
		{
			if (stage) init();
			else addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private function init(e:Event = null):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			gfx = new ColormapImage() as Bitmap;
			addChild(gfx);
			
			colorpicker = new ColorPicker();
			addChild(colorpicker);
			colorpicker.visible = false;
			
			makeShortcuts(10);
			
			cacheAsBitmap = true;
			
			addEventListener(MouseEvent.MOUSE_OVER, mouseoverHandler);
			addEventListener(MouseEvent.MOUSE_OUT, mouseoutHandler);
			addEventListener(MouseEvent.MOUSE_MOVE, mousemoveHandler);
			addEventListener(MouseEvent.CLICK, clickHandler);
			
		}
		
		private function makeShortcuts(amount:int):void 
		{
			shortcuts = new Array();
			
			graphics.beginFill(0);
			graphics.drawRect(0, 0, 210, 185);
			graphics.endFill();
			
			for (var i:int = 0; i < amount; i++) 
			{
				var shortcut:Shortcut = new Shortcut(shortcuts.length);
				addChild(shortcut);
				shortcut.addEventListener(MouseEvent.CLICK, clickHandler);
				shortcut.addEventListener(MouseEvent.MOUSE_OVER, shortcutMouseOver);
				shortcut.addEventListener(MouseEvent.MOUSE_OUT, shortcutMouseOut);
				
				shortcut.x = shortcuts.length * 12;
				shortcut.y = 175;
				shortcuts.push(shortcut);
			}
		}
		
		private function shortcutMouseOver(e:MouseEvent):void 
		{
			mouseOverShortcut = true;
			shortcutOver = e.currentTarget.index;
		}
		
		private function shortcutMouseOut(e:MouseEvent):void 
		{
			mouseOverShortcut = false;		
			shortcutOver = -1;
		}
		
		private function clickHandler(e:MouseEvent):void 
		{
			if (mouseOverShortcut) {
				chosenColor = Shortcut(shortcuts[shortcutOver]).color;
			} else {
				chosenColor = getColor();
			}
			dispatchEvent(new Event(COLOR_CHOSEN));
		}
		
		private function getColor():uint {
			
			return gfx.bitmapData.getPixel(mouseX, mouseY);
		}
		
		private function mousemoveHandler(e:MouseEvent):void 
		{
			colorpicker.x = mouseX;
			colorpicker.y = mouseY;
			colorpicker.drawPicker(getColor());
			if (mouseY > 175) {
				Mouse.show();
			} else {
				Mouse.hide();
			}
		}
		
		private function keyDownHandler(e:KeyboardEvent):void 
		{
			if (e.keyCode == 49) shortcuts[0].draw(getColor());
			if (e.keyCode == 50) shortcuts[1].draw(getColor());
			if (e.keyCode == 51) shortcuts[2].draw(getColor());
			if (e.keyCode == 52) shortcuts[3].draw(getColor());
			if (e.keyCode == 53) shortcuts[4].draw(getColor());
			if (e.keyCode == 54) shortcuts[5].draw(getColor());
			if (e.keyCode == 55) shortcuts[6].draw(getColor());
			if (e.keyCode == 56) shortcuts[7].draw(getColor());
			if (e.keyCode == 57) shortcuts[8].draw(getColor());
			if (e.keyCode == 48) shortcuts[9].draw(getColor());
		}
		
		private function mouseoverHandler(e:MouseEvent):void 
		{
			colorpicker.visible = true;
			Mouse.hide();
			stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
		}
		
		private function mouseoutHandler(e:MouseEvent):void 
		{
			colorpicker.visible = false;
			Mouse.show();
			stage.removeEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
		}
	}

}